
import { checkDouYinToken } from '@/api/third/douYin'
import { checkKuaiShouToken } from '@/api/third/kuaiShou'
import { checkTaoBaoToken } from '@/api/third/taoBao'
import { checkPinDuoDuoToken } from '@/api/third/pinDuoDuo'
import { checkTmallToken } from '@/api/third/tmall'
import { checkOpenAlibabaToken } from '@/api/third/openAlibaba'

export default {
  data() {
    return {
      // host地址
      host: '',
      // 店铺类型
      shopType: '',
      // 统一参数
      commonParams: ''
    }
  },
  mounted() {
    let host = window.location.host
    // 自定义域名转为正式环境域名
    if (host.indexOf('e.joinsun.vip') === -1 && host.indexOf('e-1.joinsun.vip') === -1 && host.indexOf('127.0.0.1') === -1 && host.indexOf('localhost') === -1) {
      host = 'e.joinsun.vip'
    }
    this.host = host
    let shopType
    let params = '~enterpriseId=' + this.getEnterpriseId()
    if (this.getEnterpriseId()) {
      // 企业店铺
      shopType = '2'
    }
    if (this.getMemberId()) {
      // 分销店铺
      shopType = '1'
      params += '~memberId=' + this.getMemberId()
    }
    if (this.getSupplierId()) {
      // 供应商店铺
      shopType = '3'
      params += '~supplierId=' + this.getSupplierId()
    }
    this.shopType = shopType
    this.commonParams = params + '~shopType=' + this.shopType
  },
  methods: {
    // 平台授权
    authByPlatform(platformType, redirectUrl) {
      const url = encodeURIComponent(redirectUrl)
      switch (platformType) {
        // 抖音
        case 1:
          this.douYinAuth(url)
          break
        // 快手
        case 2:
          this.kuaiShouAuth(url)
          break
        // 淘宝
        case 3:
          this.taoBaoAuth(redirectUrl)
          break
        // 拼多多
        case 4:
          this.pinDuoDuoAuth(url)
          break
        // 天猫
        case 5:
          this.tmallAuth(redirectUrl)
          break
        // 亚马逊
        case 6:
          this.amazonAuth(url)
          break
        // 1688
        case 7:
          this.openAlibabaAuth(url)
          break
        default:
          this.$message({ type: 'error', message: '平台类型不存在' })
      }
    },
    // 抖音授权
    douYinAuth(redirectUrl) {
      const serviceId = this.getEnterpriseConfig().douYinServiceId
      const douYinUrl = 'https://fuwu.jinritemai.com/authorize?service_id=' + serviceId + '&state='
      location.href = douYinUrl + encodeURIComponent(redirectUrl + this.commonParams)
    },
    // 快手授权
    kuaiShouAuth(redirectUrl) {
      const appId = 'ks700028268876640580'
      const scopeList = ['user_info', 'merchant_item', 'merchant_order', 'merchant_refund', 'merchant_user', 'merchant_servicemarket', 'merchant_logistics']
      const kuaiShouUrl = 'https://s.kwaixiaodian.com/oauth/authorize?appId=' + appId + '&response_type=code&scope=' + scopeList.join(',') + '&redirect_uri='
      location.href = kuaiShouUrl + 'https://' + this.host + '/api/kuaiShou/auth/redirect/1002&state=' + encodeURIComponent(redirectUrl + this.commonParams)
    },
    // 淘宝授权
    taoBaoAuth(redirectUrl) {
      const appKey = '32372331'
      const taoBaoUrl = 'https://oauth.taobao.com/authorize?response_type=code&view=web&client_id=' + appKey + '&redirect_uri='
      location.href = taoBaoUrl + 'https://' + this.host + '/api/taoBaoErp/auth/redirect.ignore&state=' + encodeURIComponent(redirectUrl + this.commonParams)
    },
    // 拼多多授权
    pinDuoDuoAuth(redirectUrl) {
      const clientId = '3420b53f1747429f9c5318775ebac12c'
      const pinDuoDuoUrl = 'https://fuwu.pinduoduo.com/service-market/auth?response_type=code&client_id=' + clientId + '&redirect_uri='
      location.href = pinDuoDuoUrl + 'https://' + this.host + '/api/pinDuoDuo/auth/redirect&state=' + encodeURIComponent(redirectUrl + this.commonParams)
    },
    // 天猫授权
    tmallAuth(redirectUrl) {
      const appKey = '32372331'
      const tmallUrl = 'https://oauth.taobao.com/authorize?response_type=code&view=tmall&client_id=' + appKey + '&redirect_uri='
      location.href = tmallUrl + 'https://' + this.host + '/api/tmall/auth/redirect.ignore&state=' + encodeURIComponent(redirectUrl + this.commonParams)
    },
    // TODO 唤起亚马逊授权
    amazonAuth(encodedRedirectUrl) {
      const applicationId = 'amzn1.sp.solution.12e52571-d7ff-4188-9022-b9b47a14a40e'// TODO 从接口取当前用户配置的
      const amazonState = 'stateexample' // 步骤3要用这个来校验
      // TODO 去掉version=beta
      const amazonUrl = 'https://sellercentral.amazon.com/apps/authorize/consent?application_id=' + applicationId + '&state=' + amazonState + '&version=beta'
      location.href = amazonUrl// + 'https://' + this.host + '/api/amazon/auth/consent' 重定向地址是卖家自己配置到亚马逊应用程序那边，所以这里不需要指定回调地址
    },
    // 1688授权
    openAlibabaAuth(redirectUrl) {
      const appKey = '2734491'
      const site = '1688'
      const alibabaYinUrl = 'https://auth.1688.com/oauth/authorize?client_id=' + appKey + '&site=' + site + '&redirect_uri='
      // const tamllAuth = 'https://openmall.joinsun.vip/api/openAlibaba/common/auth&state='
      const temp = 'https://' + this.host + '/api/openAlibaba/auth/redirect.ignore&state='
      location.href = alibabaYinUrl + temp + encodeURIComponent(redirectUrl + this.commonParams)
    },
    /**
     * 校验access_token是否存在
     * @param platformType 平台类型
     * @param shopId 店铺id
     * @param redirectUrl 回调跳转地址
     * @param isJumpAuth 校验失败是否直接跳至第三方授权界面，默认true
     */
    checkAccessToken(platformType, shopId, redirectUrl, isJumpAuth) {
      const self = this
      if (typeof isJumpAuth === 'undefined') {
        isJumpAuth = true
      }
      return new Promise(async function(resolve, reject) {
        if (!platformType) {
          self.$message({ type: 'error', message: '请先选择平台类型' })
          return resolve(false)
        }
        if (!shopId) {
          self.$message({ type: 'error', message: '请先选择店铺' })
          return resolve(false)
        }
        const type = platformType * 1
        let response
        switch (type) {
          // 抖音
          case 1:
            response = await checkDouYinToken(shopId)
            if (response.isError) return resolve(false)
            if (response.data === 0) {
              if (isJumpAuth) {
                self.douYinAuth(redirectUrl)
              }
              return resolve(false)
            }
            break
          // 快手
          case 2:
            response = await checkKuaiShouToken(shopId)
            if (response.isError) return resolve(false)
            if (response.data === 0) {
              if (isJumpAuth) {
                self.kuaiShouAuth(redirectUrl)
              }
              return resolve(false)
            }
            break
          // 淘宝
          case 3:
            response = await checkTaoBaoToken(shopId)
            if (response.isError) return resolve(false)
            if (response.data === 0) {
              if (isJumpAuth) {
                self.taoBaoAuth(redirectUrl)
              }
              return resolve(false)
            }
            break
          // 拼多多
          case 4:
            response = await checkPinDuoDuoToken(shopId)
            if (response.isError) return resolve(false)
            if (response.data === 0) {
              if (isJumpAuth) {
                self.pinDuoDuoAuth(redirectUrl)
              }
              return resolve(false)
            }
            break
          // 天猫
          case 5:
            response = await checkTmallToken(shopId)
            if (response.isError) return resolve(false)
            if (response.data === 0) {
              if (isJumpAuth) {
                self.tmallAuth(redirectUrl)
              }
              return resolve(false)
            }
            break
          // 1688
          case 7:
            response = await checkOpenAlibabaToken(shopId)
            if (response.isError) return resolve(false)
            if (response.data === 0) {
              if (isJumpAuth) {
                self.openAlibabaAuth(redirectUrl)
              }
              return resolve(false)
            }
            break
          default:
            return resolve(false)
        }
        return resolve(true)
      })
    }
  }
}
